package com.ruoyi.mobile.project.sms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.sms.domain.SmsCouponUser;
import com.ruoyi.db.sms.domain.query.SmsCouponUserQueryVo;
import com.ruoyi.db.sms.mapper.SmsCouponUserMapper;
import com.ruoyi.mobile.project.utils.CouponUserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WxSmsCouponUserService
{
    @Autowired
    private SmsCouponUserMapper couponUserMapper;
//蔡江恩
    public Integer countCoupon(Long couponId)
    {
        SmsCouponUserQueryVo smsCouponUserQueryVo=new SmsCouponUserQueryVo();

        return couponUserMapper.selectSmsCouponUserList(smsCouponUserQueryVo).size();
    }
//蔡江恩
    public Integer countUserAndCoupon(Long userId, Long Long)
    {
        int n=0;
        SmsCouponUserQueryVo smsCouponUserQueryVo=new SmsCouponUserQueryVo();
        int z=couponUserMapper.selectSmsCouponUserList(smsCouponUserQueryVo).size();
        for(int i=0;  i<z; i++){
            couponUserMapper.selectSmsCouponUserList(smsCouponUserQueryVo).get(i).getCouponId();
            long a=couponUserMapper.selectSmsCouponUserList(smsCouponUserQueryVo).get(i).getCouponId();
            if(a==Long){
                n++;
            }
        }
        return n;
    }
//蔡江恩
    public void add(SmsCouponUser couponUser)
    {
        couponUser.setAddTime(LocalDateTime.now());
        couponUser.setUpdateTime(LocalDateTime.now());
        couponUserMapper.insertSmsCouponUser(couponUser);
    }

    /**
     * 条件查询
     *
     * @author 严方亚
     * @date 2020-05-22
     */
    public List<SmsCouponUser> queryList(Long userId, Long couponId, Integer status, Integer page, Integer size, String sort, String order)
    {
        SmsCouponUserQueryVo smsCouponUserQueryVo = new SmsCouponUserQueryVo();
        if (userId != null) {
            smsCouponUserQueryVo.setUserId(userId);
        }
        if(couponId != null){
            smsCouponUserQueryVo.setCouponId(couponId);
        }
        if (status != null) {
            smsCouponUserQueryVo.setStatus(status);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            smsCouponUserQueryVo.setSort(sort);
            smsCouponUserQueryVo.setOrder(order);
        }

        if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
            PageHelper.startPage(page, size);
        }

        return couponUserMapper.selectSmsCouponUserList(smsCouponUserQueryVo);
    }

    public List<SmsCouponUser> queryAll(Long userId, Long couponId)
    {
        return null;
    }

    public List<SmsCouponUser> queryAll(Long userId)
    {
        return queryList(userId, null, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
    }

    /**
     * 查询出来后选择一个
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     * @param couponId
     */
    public SmsCouponUser queryOne(Long userId, Long couponId)
    {
        List<SmsCouponUser> couponUserList = queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, 1, 1, "add_time", "desc");
        if(couponUserList.size() == 0){
            return null;
        }
        return couponUserList.get(0);
    }

    /**
     * 查询用户优惠券
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param id
     */
    public SmsCouponUser findById(Long id)
    {
        return couponUserMapper.selectSmsCouponUserById(id);
    }

    /**
     * 更新用户优惠券
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param couponUser
     */
    public int update(SmsCouponUser couponUser)
    {
        couponUser.setUpdateTime(LocalDateTime.now());
        return couponUserMapper.updateSmsCouponUser(couponUser);
    }

    public List<SmsCouponUser> queryExpired()
    {
        return null;
    }
}
